﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fyp.Filters;
using System.Web.Security;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using Fyp.Models;

namespace Fyp.Controllers
{
    public class MeetingMinuteController : Controller
    {
        //
        // GET: /MeetingMinute/

        public ActionResult MeetingList(int? id)
        {
            try
            {
                var db = new FYPEntities5();

                var userId = WebSecurity.CurrentUserId;
                var userName = WebSecurity.CurrentUserName;

                var getGid = (from g in db.Students
                              where userName == g.Std_Email
                              select g.GId).FirstOrDefault();

                IQueryable<MeetingMinute> list = null;

                if (WebSecurity.IsAuthenticated && Roles.IsUserInRole("Advisor"))
                {
                    list = from m in db.MeetingMinutes
                           select m;
                }
                else
                {
                    list = from m in db.MeetingMinutes
                           where getGid == m.GId
                           select m;
                }


                if (list != null)
                {
                    ViewData["data"] = list.ToList();
                }
            }
            catch (Exception e)
            {

                Console.WriteLine(e.InnerException);
            }
            return View("MeetingList");
        }


       public ActionResult MeetingMinute(int? id)
        {
            try
            {
                var db = new FYPEntities5();

                if (id == null || id == 0)
                {
                    return View("MeetingMinute");
                }
                else
                {
     
                    var minutes = from mms in db.MeetingMinutes
                              where mms.MeetingMinuteId == id
                              select mms;
                    
                    ViewData["minutes"] = minutes.ToList();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
            } 
            
            return View("MeetingMinute");
        }

       public ActionResult SearchMeeting()
       {
           return View("SearchMeeting");
       }


       [HttpPost]
       public ActionResult SearchMeeting(FormCollection c)
       {
           var word = c["search"];
           var db = new FYPEntities5();
           try
           {
               IQueryable<MeetingMinute> q = from mm in db.MeetingMinutes
                                                where mm.MMTitle.Contains(word)
                                                select mm;
               ViewData["data"] = q.ToList();
           }
           catch (Exception e)
           {
               Console.WriteLine(e.InnerException);
           }
           return View("MeetingList");
       }


        [HttpPost]
        public ActionResult MeetingMinute(FormCollection c)
        {
            var db = new FYPEntities5();

            try
            {
                MeetingMinute mms = new MeetingMinute();
                mms.MeetingMinuteId = Convert.ToInt32(c["ID"]);
                mms.MMTitle = c["mTitle"];
                mms.MMDate = DateTime.Parse(c["mdate1"]);
                mms.MMTime = c["mtime1"];
                mms.MMLocation = c["mlocation"];
                mms.MCalledBy = c["mCalledBy"];
                mms.MMType = c["mtype"];
                mms.Attendees = c["mAttendees"];
                mms.TimeAlloted = c["timeAlloted"];
                mms.AgendaTopic = c["agendaTopics"];
                mms.Presenter = c["presenter"];
                mms.DiscussionText = c["discussion"];
                mms.CommentText = c["comments"];
                mms.GId = Convert.ToInt32(c["Gid"]);

                var mmId = mms.MeetingMinuteId;
                var mtime = mms.MMTime;
                var mtitle = mms.MMTitle;
                var mlocation = mms.MMLocation;
                var mdate = mms.MMDate;
                var mcalledby = mms.MCalledBy;
                var mtype = mms.MMType;
                var mattendees = mms.Attendees;
                var mtimealloted = mms.TimeAlloted;
                var magenda = mms.AgendaTopic;
                var mpresenter = mms.Presenter;
                var mdiscussion = mms.DiscussionText;
                var mcomment = mms.CommentText;
                var mgid = mms.GId;

                ViewData["mtime"] = mtime;
                ViewData["mtitle"] = mtitle;
                ViewData["mlocation"] = mlocation;
                ViewData["mdate"] = mdate.Value.ToString("dd/MM/yyyy");
                ViewData["mcalledby"] = mcalledby;
                ViewData["mtype"] = mtype;
                ViewData["mattendees"] = mattendees;
                ViewData["mtimealloted"] = mtimealloted;
                ViewData["magenda"] = magenda;
                ViewData["mpresenter"] = mpresenter;
                ViewData["mdiscussion"] = mdiscussion;
                ViewData["mcomment"] = mcomment;
                ViewData["mgid"] = mgid;

           if (mms.MeetingMinuteId == null || mms.MeetingMinuteId == 0)
           {
               db.MeetingMinutes.Add(mms);
               db.SaveChanges();
           }
           else
           {
               var q = from mm in db.MeetingMinutes
                       where mm.MeetingMinuteId == mmId
                       select mm;

               foreach (var val in q)
               {
                   val.MMTitle = mtitle;
                   val.MMDate = mdate;
                   val.MMTime = mtime;
                   val.MMLocation = mlocation;
                   val.MCalledBy = mcalledby;
                   val.MMType = mtype;
                   val.TimeAlloted = mtimealloted;
                   val.AgendaTopic = magenda;
                   val.Presenter = mpresenter;
                   val.DiscussionText = mdiscussion;
                   val.CommentText = mcomment;
                   val.GId = mgid;
               }

               db.SaveChanges();
           }
        }

        catch (Exception e)
        {
                Console.WriteLine(e.InnerException);
        }
            return View("MeetingMinute");
        }
    }
}
